#include "Game.h"

#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000)? 1: 0)

Game::Game(void)
{
	
}

Game::~Game(void)
{
	this->GameEnd();
}

Game::Game(HINSTANCE hInstance, LPCSTR GameName, LPSTR lpCmdLine, int nShowCmd, int FPS, int ScreenWidth, int ScreenHeight)
{
	this->_hInstance = hInstance;
	this->_lpCmdLine = lpCmdLine;
	this->_nShowCmd = nShowCmd;
	this->_gameName = GameName;
	this->_FPS = FPS;
	this->_x = 0;
	this->_y = 100;
	this->_vx = 1;
	this->_coll = Collision();

	this->_screenWidth = ScreenWidth;
	this->_screenHeight = ScreenHeight;
}

LRESULT CALLBACK Game::WinProc(HWND _hWnd,UINT msg,WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;

	case WM_ACTIVATE:
		if ((int)WA_ACTIVE == wParam)
		{
			
		}

	}
	return DefWindowProc(_hWnd,msg,wParam,lParam);
}

int Game::InitWindow()
{
	WNDCLASS 	wc;		

	wc.style         = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc   = (WNDPROC)Game::WinProc;
	wc.cbClsExtra    = 0;
	wc.cbWndExtra    = 0;
	wc.hInstance     = _hInstance;
	wc.hIcon         = ::LoadIcon(0, IDI_APPLICATION);
	wc.hCursor       = ::LoadCursor(0, IDC_ARROW);
	wc.hbrBackground = static_cast<HBRUSH>(::GetStockObject(WHITE_BRUSH));
	wc.lpszMenuName  = NULL;
	wc.lpszClassName = this->_gameName;

	if(!RegisterClass(&wc))
	{
		MessageBox(0, TEXT("RegisterClass - Failed"), 0, 0);
		return 0;
	}
	
	_hWnd = CreateWindow(wc.lpszClassName,
						wc.lpszClassName,
						WS_OVERLAPPEDWINDOW,
						//WS_EX_TOPMOST|WS_VISIBLE|WS_POPUP,
						CW_USEDEFAULT,
						CW_USEDEFAULT,
						800,
						600,
						NULL,
						NULL,
						_hInstance,
						0);
	if (_hWnd == 0)
	{
		MessageBox(NULL,"Cannot create window!","Error",MB_OK);
		return 0;
	}

	ShowWindow(_hWnd,_nShowCmd);
	UpdateWindow(_hWnd);

	return 0;
}

int Game::GameInit()
{
	this->InitWindow();

	//Init DirectX
	_directX = new DirectX();
	_directX->setHandle(this->_hWnd);
	_directX->InitDirect3D(_screenWidth,_screenHeight);

	//Init DirectInput
	_dInput = new DXinput(_hInstance,_hWnd);
	_dInput->InitDirectInput();
	_dInput->InitKeyboard();

	this->LoadResource();
	return 1;
}

void Game::GameLoop()
{
	MSG msg;
	long Tick = GetTickCount();

	while (true)
	{
		if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
		{
			if (msg.message == WM_QUIT)
				break;
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{
			GameRender();
			this->_dInput->PollKeyboard();
			ProcessKeyboard();
			if (GetTickCount() - Tick > 1000/_FPS)
			{
					GameUpdate();
					Tick = GetTickCount();
			}
			

		}
	}
}

void Game::GameUpdate()
{
	
}

void Game::GameRender()
{
	LPDIRECT3DDEVICE9 dev = _directX->getDevice();
	if (dev == NULL)
	{
		return;
	}
	dev->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);

	LPDIRECT3DSURFACE9 backbuffer = _directX->getBackBuffer();

	if (dev->BeginScene())
	{
		_directX->getSpriteHandler()->Begin(D3DXSPRITE_ALPHABLEND);
		RenderFrame();
		_directX->getSpriteHandler()->End();
		dev->EndScene();
	}

	dev->Present(NULL,NULL,NULL,NULL);
	if (KEY_DOWN(VK_ESCAPE))
		PostMessage(_hWnd,WM_DESTROY,0,0);
}

void Game::GameEnd()
{
	delete _directX;
	delete _dInput;
}

void Game::ProcessKeyboard()
{
	for (int i=0;i<256;i++)
	{
		if (this->_dInput->isKeyPress(i))
			OnKeyDown(i);
	}
}